package com.ys.day30.p4XML解析与配置;

import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

/**
 * 描述: P2解析案列
 *   使用XML 解析来模拟客户端,服务端通信
 *
 * @author CodeYang
 * @version 2021/1/13 0:17
 */
public class P2解析案列 {
}

/**
 * 服务器套接字
 */
class ServerSocket01{

    public static void main(String[] args) {
        // 根据key来获得文件配置信息 ,用于注册服务
        SocketBean socketBean = SocketUtils.getSocketBean("socket01");

        try(
                ServerSocket serverSocket = new ServerSocket(socketBean.getPort());
                //监听
                 Socket accept = serverSocket.accept();
                 //获得 out
                OutputStream outputStream = accept.getOutputStream();
                // 装饰一下
                BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(outputStream));

        )
         {
             LocalDateTime now = LocalDateTime.now();
             //格式化
             DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
             String datatime = dateTimeFormatter.format(now);
             // 发送一个 数据
             bw.write(datatime);
             // 结束标志
             bw.newLine();
             bw.flush();


         }catch(Exception e){
            System.out.println("server error");
            e.printStackTrace();
        }
    }
}

/**
 * 客户端套接字
 */
class ClientSocket01{
    public static void main(String[] args) {
        // 根据key 来获取配置文件
        SocketBean socketBean = SocketUtils.getSocketBean("socket01");

        try(
                Socket socket = new Socket(socketBean.getAddress(),socketBean.getPort());
                InputStream inputStream = socket.getInputStream();
                BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
        )
         {
         // 读取server msg
             String msg = bufferedReader.readLine();
             System.out.println(msg);


         }catch(Exception e){
            System.out.println("client error");
            e.printStackTrace();
        }
    }
}
